﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        private string name = "";
        public Form2(string namestr)
        {
            name = namestr;
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            this.StartPosition = FormStartPosition.CenterScreen;
            Mydb mydb = new Mydb();
            string G_Fault = mydb.getG_Fault(name).Rows[0]["G_Fault"].ToString().Replace("[^]", "'"); ;
            string G_Name = mydb.getG_Fault(name).Rows[0]["G_Name"].ToString();

            string exePath = System.Environment.CurrentDirectory + @"\html\"+getTreeNumRandom();
            
            shengcheng(G_Name, G_Fault, exePath);
          
            webBrowser1.Navigate(exePath, false);
            mydb.coleconn();
        }

        public void shengcheng(string G_Name, string G_Fault, string exePath) 
        {
            try 
            {
                StreamWriter sw = new StreamWriter(exePath, false);
                sw.WriteLine(getStr(G_Name, G_Fault));
                sw.Flush();
                sw.Close();

                System.IO.StreamReader st;
                st = new System.IO.StreamReader(exePath, System.Text.Encoding.UTF8);//UTF8为编码
            }
            catch 
            {
                this.Close();
            }
        }
        
        public string getStr(string name,string connStr)
        {
            string retStr = "";
            //connStr = connStr.Replace("<", "&lt;");
            //connStr = connStr.Replace(">", "&gt;");
            //<meta http-equiv='X-UA-Compatible' Content='IE=edge'>

retStr = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>";
retStr += "<html xmlns='http://www.w3.org/1999/xhtml'><head><meta http-equiv='X-UA-Compatible' Content='IE=edge'><meta charset='utf-8'><title>Layui</title><style>.layui-code-ol{font-size:17px;}</style>";
retStr += "<link rel='stylesheet' href='layui/css/layui.css' media='all'><script src='layui/layui.js' charset='utf-8'></script></head>";
retStr += "<body style='margin:0px;padding:0px;background-color: #000;'><pre class='layui-code' lay-encode='true' lay-skin='notepad' lay-title='" + name + "' style='margin: 0px 0px;'>";
retStr += connStr;
retStr += "</pre><script type='text/javascript'>layui.use(['code'], function () {layui.code({ about: false });});</script></body></html>";

            return retStr;
        }
        
        /// <summary>
        /// 随机数
        /// </summary>
        /// <returns></returns>
        public string getTreeNumRandom()
        {
            string time = DateTime.Now.ToFileTime().ToString();
            Random ran = new Random();
            int RandKey = ran.Next(100, 999);
            return time + RandKey.ToString()+".html";
        }

        protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
        {

            int WM_KEYDOWN = 256;

            int WM_SYSKEYDOWN = 260;

            if (msg.Msg == WM_KEYDOWN | msg.Msg == WM_SYSKEYDOWN)
            {
                switch (keyData)
                {
                    case Keys.Escape:
                        this.Close();//esc关闭窗体
                        break;
                }
            }
            return false;
        }



    }
}
